package com.sc.rest.service.impl;

import com.sc.common.utils.JsonUtils;
import com.sc.mapper.TbContentMapper;
import com.sc.pojo.TbContent;
import com.sc.pojo.TbContentExample;
import com.sc.rest.component.JedisClient;
import com.sc.rest.service.ContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by sc on 2017/10/19.
 */
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper tbContentMapper;
    @Autowired
    private JedisClient jedisClient;
    @Value("${REDIS_CONTENT_KEY}")
    private String REDIS_CONTENT_KEY;
    @Override
    public List<TbContent> getlist(Long id) {
        try {
            String json = jedisClient.hget(REDIS_CONTENT_KEY,id.toString());
            if(!StringUtils.isBlank(json)){
                List<TbContent>  tbContentList = JsonUtils.jsonToList(json,TbContent.class);
                return tbContentList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(id);
        List<TbContent> tbContentList = tbContentMapper.selectByExampleWithBLOBs(example);

        try {
            jedisClient.hset(REDIS_CONTENT_KEY,id.toString(), JsonUtils.objectToJson(tbContentList));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tbContentList;
    }
}
